<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代码" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="zoneList"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="handleLoad"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      >
      <el-table-column label="名称" prop="name" width="300"/>
      <el-table-column label="代码" align="center" prop="id"/>
      <el-table-column label="扩展代码" align="center" prop="extId" />
      <el-table-column label="扩展名称" align="center" prop="extName" />
      <el-table-column label="级别" align="center" prop="level">
        <template #default="scope">
          <dict-tag :options="dict.type.CityLevel" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName"/>
      <el-table-column label="描述" align="center" prop="description"/>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.CommDataStatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getChildZone, getZone } from '@/api/kysys/zone'

export default {
  name: 'Zone',
  dicts: ['CommDataStatus', 'CityLevel'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 行政区域表格数据
      zoneList: [],
      // 行政区域树选项
      zoneOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: null,
        level: null,
        id: null,
        zid: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询行政区域列表 */
    getList() {
      this.loading = true
      getChildZone(this.queryParams).then(response => {
        this.loading = false
        this.zoneList = response.data
      })
    },
    /** 转换行政区域数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        latitude: null,
        level: null,
        zid: null,
        status: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.id) {
        this.form.zid = row.id
      } else {
        this.form.zid = 0
      }
      this.open = true
      this.title = '添加行政区域'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.zid = row.id
      }
      getZone(row.id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改行政区域'
      })
    },
    handleLoad(tree, treeNode, resolve) {
      this.loading = true
      getChildZone({zid: tree.id}).then(response => {
        this.loading = false
        resolve(response.data)
      })
    }
  }
}
</script>
